package com.kalvin.kvf.modules.hxy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kalvin.kvf.common.utils.ShiroKit;
import com.kalvin.kvf.modules.dy.entity.IntentionCust;
import com.kalvin.kvf.modules.dy.entity.Task;
import com.kalvin.kvf.modules.dy.entity.Video;
import com.kalvin.kvf.modules.dy.mapper.IntentionCustMapper;
import com.kalvin.kvf.modules.dy.mapper.TaskMapper;
import com.kalvin.kvf.modules.dy.mapper.VideoMapper;
import com.kalvin.kvf.modules.ks.entity.KSIntentionCust;
import com.kalvin.kvf.modules.ks.entity.KSTask;
import com.kalvin.kvf.modules.ks.entity.KSVideo;
import com.kalvin.kvf.modules.ks.mapper.KSIntentionCustMapper;
import com.kalvin.kvf.modules.ks.mapper.KSTaskMapper;
import com.kalvin.kvf.modules.ks.mapper.KSVideoMapper;
import com.kalvin.kvf.modules.sys.entity.User;
import com.kalvin.kvf.modules.sys.service.IUserService;
import com.kalvin.kvf.modules.xhs.entity.XhsIntentionCust;
import com.kalvin.kvf.modules.xhs.entity.XhsTask;
import com.kalvin.kvf.modules.xhs.entity.XhsVideo;
import com.kalvin.kvf.modules.xhs.mapper.XhsIntentionCustMapper;
import com.kalvin.kvf.modules.xhs.mapper.XhsTaskMapper;
import com.kalvin.kvf.modules.xhs.mapper.XhsVideoMapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.kalvin.kvf.common.controller.BaseController;
import com.kalvin.kvf.common.dto.R;
import com.kalvin.kvf.modules.hxy.entity.Account;
import com.kalvin.kvf.modules.hxy.service.AccountService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 账户信息表 前端控制器
 * </p>
 * @since 2023-10-28 15:35:38
 */
@RestController
@RequestMapping("hxy/account")
public class AccountController extends BaseController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private IUserService userService;
    @Autowired
    private TaskMapper taskMapper;
    @Autowired
    private IntentionCustMapper intentionCustMapper;
    @Autowired
    private VideoMapper videoMapper;
    @Autowired
    private KSTaskMapper ksTaskMapper;
    @Autowired
    private XhsTaskMapper xhsTaskMapper;
    @Autowired
    private KSIntentionCustMapper ksIntentionCustMapper;
    @Autowired
    private XhsIntentionCustMapper xhsIntentionCustMapper;
    @Autowired
    private KSVideoMapper ksVideoMapper;
    @Autowired
    private XhsVideoMapper xhsVideoMapper;

    @RequiresPermissions("hxy:account:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("hxy/account");
    }

    @GetMapping(value = "edit")
    public ModelAndView edit(Long id) {
        ModelAndView mv = new ModelAndView("hxy/account_edit");
        Account account;
        if (id == null) {
            account = new Account();
        } else {
            account = accountService.getById(id);
        }
        mv.addObject("editInfo", account);
        return mv;
    }

    @GetMapping(value = "list/data")
    public R listData(Account account) {
        Page<Account> page = accountService.listAccountPage(account);
        return R.ok(page);
    }

    @RequiresPermissions("hxy:account:add")
    @PostMapping(value = "add")
    public R add(Account account) throws Exception {
        //校验手机号是否存在
        User userName = userService.getOne(new QueryWrapper<User>().eq("username", account.getPhone()));
        if (userName == null){
            throw new Exception("手机号不存在");
        }
        //校验手机号是否在account已存在
        Account phone = accountService.getOne(new QueryWrapper<Account>().eq("phone", account.getPhone()));
        if (phone!=null){
            throw new Exception("手机号已存在");
        }
        accountService.save(account);
        return R.ok();
    }

    @RequiresPermissions("hxy:account:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        accountService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("hxy:account:edit")
    @PostMapping(value = "edit")
    public R edit(Account account) {
        accountService.updateById(account);
        return R.ok();
    }

    @RequiresPermissions("hxy:account:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        accountService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(accountService.getById(id));
    }

    @PostMapping (value = "account")
    public Map<String,Integer> userAccount() throws Exception {
        //总任务数
        Integer dyTaskCount = taskMapper.selectCount(new QueryWrapper<Task>().eq("phone", ShiroKit.getUser().getUsername()));
        Integer ksTaskCount = ksTaskMapper.selectCount(new QueryWrapper<KSTask>().eq("phone", ShiroKit.getUser().getUsername()));
        Integer xhsTaskCount = xhsTaskMapper.selectCount(new QueryWrapper<XhsTask>().eq("phone", ShiroKit.getUser().getUsername()));
        int taskCount=dyTaskCount+ksTaskCount+xhsTaskCount;
        //运行任务数量
        Integer dyRunTaskCount = taskMapper.selectCount(new QueryWrapper<Task>().eq("phone", ShiroKit.getUser().getUsername()).eq("status",0));
        Integer ksRunTaskCount = ksTaskMapper.selectCount(new QueryWrapper<KSTask>().eq("phone", ShiroKit.getUser().getUsername()).eq("status",0));
        Integer xhsRunTaskCount = xhsTaskMapper.selectCount(new QueryWrapper<XhsTask>().eq("phone", ShiroKit.getUser().getUsername()).eq("status",0));
        int runTaskCount=dyRunTaskCount+ksRunTaskCount+xhsRunTaskCount;
        //获取客户数量
        Integer dyIntentionCustCount = intentionCustMapper.selectCount(new QueryWrapper<IntentionCust>().eq("phone", ShiroKit.getUser().getUsername()));
        Integer ksIntentionCustCount = ksIntentionCustMapper.selectCount(new QueryWrapper<KSIntentionCust>().eq("phone", ShiroKit.getUser().getUsername()));
        Integer xhsIntentionCustCount = xhsIntentionCustMapper.selectCount(new QueryWrapper<XhsIntentionCust>().eq("phone", ShiroKit.getUser().getUsername()));
        int intentionCustCount=dyIntentionCustCount+ksIntentionCustCount+xhsIntentionCustCount;
        //统计视频数量
        Integer dyVideoCount = videoMapper.selectCount(new QueryWrapper<Video>().eq("phone", ShiroKit.getUser().getUsername()));
        Integer ksVideoCount = ksVideoMapper.selectCount(new QueryWrapper<KSVideo>().eq("phone", ShiroKit.getUser().getUsername()));
        Integer xhsVideoCount = xhsVideoMapper.selectCount(new QueryWrapper<XhsVideo>().eq("phone", ShiroKit.getUser().getUsername()));
        int videoCount=dyVideoCount+ksVideoCount+xhsVideoCount;
        //消耗点数
        int xhCustNum=dyIntentionCustCount+ksIntentionCustCount+xhsIntentionCustCount;
        //剩余点数
        System.out.println(ShiroKit.getUser().getUsername());
        Account account = accountService.getOne(new QueryWrapper<Account>().eq("phone", ShiroKit.getUser().getUsername()));
        if (account==null){
            throw new Exception("未配置账户信息，请联系管理员！");
        }
        Map<String,Integer> map=new HashMap<>();
        map.put("taskCount",taskCount);
        map.put("runTaskCount",runTaskCount);
        map.put("intentionCustCount",intentionCustCount);
        map.put("videoCount",videoCount);
        map.put("xhCustNum",xhCustNum);
        map.put("custNum",account.getCustNum());
        System.out.println(map);
        return map;

    }

}

